"use strict"
//充值统计1
const P = require('bluebird')
const moment = require('moment')
const mvc = require("cosjs.mvc");
const timeZone = mvc.config('base', 'timeZone')
const sid = mvc.config.get('sid')
exports.page = function () {
    let uid = this.get('uid', 'string')
    let ids = uid.split(',')

    let body = this.req.body;



    let format = mvc.format("orderS");
    let query = this.library.call(this, 'jqgrid/query', body, format) || {}

    if (ids.length > 0 && ids[0]) {
        query['uid'] = {
            $nin: ids
        }
    }


    //补丁
    if (query.time) {
        timeChange(query.time);
    } else {
        if (query['$or']) {
            query['$or'].map(i => timeChange(i.time));
        }
        if (query['$and']) {
            query['$and'].map(i => timeChange(i.time));
        }

    }






    //     console.log(body);

    // //时间先转换
    // for(let k of body.filters.rules){
    //     if(k.field==='time'){
    //         if(typeof(k.data)=='string'){
    //             let _date=new Date(k.data);
    //             k.data=_date.getTime();
    //         }
    //     }
    // }

    //先打补丁



    let data = {
        "page": 1,
        "size": 1,
        "total": 1,
        "records": 1,
        "rows": []
    };

    let task = []

    for (let i of sid) {
        let model = mvc.model.mongo('order', i)
        task.push(
            model.aggregate([
                { $match: query },
                { $group: { _id: null, sum: { $sum: '$val' } } }
            ], {}),
        )
    }
    return P.all(task)
        .then(ret => {
            let sum = 0
            for (let i of ret) {
                if (i['null']) {
                    sum += i['null']['sum']
                }
            }
            if (query['$and']) {
                query.time = Object.assign({}, query['$and'][0]['time'], query['$and'][1]['time'])
            }
            let time = '', bttime = ''
            if (query.time) {
                if (query.time.$gt || query.time.$gte) {
                    time += moment(new Date(query.time.$gt || query.time.$gte)).format('YYYY-MM-DD HH:mm')
                    bttime += moment(new Date((query.time.$gt || query.time.$gte) - (timeZone - 8) * 3600000)).format('YYYY-MM-DD HH:mm')
                } else {
                    time += '开服'
                    bttime += '开服'
                }
                if (query.time.$lt || query.time.$lte) {
                    time += ' - ' + moment(new Date(query.time.$lt || query.time.$lte)).format('YYYY-MM-DD HH:mm')
                    bttime += ' - ' + moment(new Date((query.time.$lt || query.time.$lte) - (timeZone - 8) * 3600000)).format('YYYY-MM-DD HH:mm')
                } else {
                    time += ' - 现在'
                    bttime += ' - 现在'
                }
            }

            data.rows.push({
                sum: sum,
                time: time,
                bttime: bttime
            })
            return data
        })
}

function timeChange(_data_obj) {
    const keys = Object.keys(_data_obj);
    for (let k of keys) {
        if (_data_obj[k].indexOf('-') != -1) {
            let _date = new Date(_data_obj[k]);
            return _data_obj[k] = _date.getTime();
        }
    }
}


